import 'package:shared_preferences/shared_preferences.dart';

///Create Date:2021/8/6 19:42
///Author: Administrator

class SpUtil{
  
  static SpUtil get instance => _getInstance();
  
  static SpUtil? _spUtil;

  SharedPreferences? _prefs;
  
  static SpUtil _getInstance() {
    if (_spUtil == null) {
      _spUtil = new SpUtil._init();
    }
    return _spUtil!;
  }
  
  SpUtil._init(){
    if(_prefs==null){
      _initSharedPreferences();
    }
  }

  init(){
    _initSharedPreferences();
  }

  Future<void> _initSharedPreferences() async {
    _prefs = await SharedPreferences.getInstance();
  }
  
  saveUserName(String? userName){
    if(userName==null){
      return;
    }
    _prefs?.setString("user_name", userName);
  }
  saveUserAvatar(String? userAvatar){
    if(userAvatar==null){
      return;
    }
    _prefs?.setString("user_avatar", userAvatar);
  }
  saveImToken(String? token){
    if(token==null){
      return;
    }
    _prefs?.setString("user_token", token);
  }

  saveImId(String? id){
    if(id==null){
      return;
    }
    _prefs?.setString("user_id", id);
  }

  saveHttpToken(String? token){
    if(token==null){
      return;
    }
    _prefs?.setString("http_token", token);
  }

  saveBaseApi(String? baseApi){
    if(baseApi==null){
      return;
    }
    _prefs?.setString("base_api", baseApi);
  }

  String? getBaseApi(){
    return _prefs?.getString('base_api');
  }

  String? getUserName(){
    return get<String>('user_name');
  }

  String? getUserAvatar()   {
    return get<String>('user_avatar');
  }

  String? getImToken()   {
    return get<String>('user_token');
  }

  String? getImId()  {
    return get<String>('user_id');
  }

  String? getHttpToken()  {
    return get<String>('http_token');
  }

  saveRedPacketWhite(bool? isWhite)  {
    if(isWhite==null){
      return;
    }
    _prefs?.setBool("is_red_packet_white", isWhite);
  }

  bool getIsRedPacketWhite()  {
    return get('is_red_packet_white')??false;
  }

  saveMemberId(String? memberId){
    if(memberId==null){
      return;
    }
    _prefs?.setString('member_id', memberId);
  }

  String? getMemberId()  {
    return get<String>('member_id');
  }

  saveLifeCircleUserId(String? userId)  {
    if(userId==null){
      return;
    }
    _prefs?.setString('life_circle_user_id', userId);
  }

  saveLifeCircleUserName(String? userName)  {
    if(userName==null){
      return;
    }
    _prefs?.setString('life_circle_user_name', userName);
  }

  saveLifeCircleUserPortrait(String? userPortrait)  {
    if(userPortrait == null){
      return;
    }
    _prefs?.setString('life_circle_user_portrait', userPortrait);
  }

  String? getLifeCircleUserId()  {
    return get<String>('life_circle_user_id');
  }

  String? getLifeCircleUserName()  {
    return get<String>('life_circle_user_name');
  }

  String? getLifeCircleUserPortrait()  {
    return get<String>('life_circle_user_portrait');
  }

  int? getFriendNumber()   {
    return  get('friendNum')??0;
  }

  saveFriendNumber(int? friendNum)  {
    if(friendNum == null){
      return 0;
    }
    _prefs?.setInt('friendNum', friendNum);
  }
  void clear(){
    _prefs?.clear();
  }

  T? get<T> (String key) {
    var result = _prefs?.get(key);
    if (result != null) {
      return result as T;
    }
    return null;
  }
}